﻿package ascript.components.willard
{
	//#########################
	// Header Includes
	//#########################
	import ascript.components.input.InputBase;
	import ascript.utilities.PointSimple;
	import ascript.Game;
	import ascript.GameObject;
	import Box2D.Common.Math.b2Vec2;
	
	//#########################
	// InputPlayer Class
	//#########################
	public class WillardInput extends InputBase
	{
		//=========================
		// Constructors
		//=========================	
		public function WillardInput():void
		{
		}
		
		//=========================
		// Members
		//=========================
		override public function initComponent( tGame:Game, tObject:GameObject ):void
		{
			// Inheritance
			super.initComponent( tGame, tObject );
			
			// Set Default Attributes
			defaultAttribute( tObject, "InputDirection", new PointSimple( 0, 0 ) );
		}
		
		override public function applyAttribute( tGame:Game, tObject:GameObject, tAttribute:String ):void
		{
			switch( tAttribute )
			{
				case "InputDirection":
					var tempVector:b2Vec2 = new b2Vec2( tObject.getAttribute( "InputDirection" ).x, tObject.getAttribute( "InputDirection" ).y );
					tempVector.Normalize();
					tObject.setAttribute( "Direction", tempVector );
					break;
				default:
					super.applyAttribute( tGame, tObject, tAttribute );
					break;
			}
		}
		
		override protected function executeInput( tGame:Game, tObject:GameObject, tKeyCode:uint, tIsPressed:Boolean ):void
		{
			switch( tKeyCode )
			{
				case 27: // escape
					if ( tIsPressed )
					{
						if ( tGame.paused )
						{
							tGame.pauseGame( false );
						}
						else
						{
							tGame.pauseGame( true );
						}
					}
					break;
				case 38: // up arrow
				case 87: // w
					if ( tIsPressed && tObject.getAttribute( "InputDirection" ).y != -1 )
					{
						tObject.setAttribute( "InputDirection", new PointSimple( tObject.getAttribute( "InputDirection" ).x, -1 ) );
					}
					else if ( !tIsPressed && tObject.getAttribute( "InputDirection" ).y == -1 )
					{
							tObject.setAttribute( "InputDirection", new PointSimple( tObject.getAttribute( "InputDirection" ).x, 0 ) );
					}
					break;
				case 37: // left arrow
				case 65: // a
					if ( tIsPressed && tObject.getAttribute( "InputDirection" ).x != -1 )
					{
						tObject.setAttribute( "InputDirection", new PointSimple( -1, tObject.getAttribute( "InputDirection" ).y ) );
					}
					else if ( !tIsPressed && tObject.getAttribute( "InputDirection" ).x == -1 )
					{
						tObject.setAttribute( "InputDirection", new PointSimple( 0, tObject.getAttribute( "InputDirection" ).y ) );
					}
					break;
				case 39: // right arrow
				case 68: // d
					if ( tIsPressed && tObject.getAttribute( "InputDirection" ).x != 1 )
					{
						tObject.setAttribute( "InputDirection", new PointSimple( 1, tObject.getAttribute( "InputDirection" ).y ) );
					}
					else if ( !tIsPressed && tObject.getAttribute( "InputDirection" ).x == 1 )
					{
						tObject.setAttribute( "InputDirection", new PointSimple( 0, tObject.getAttribute( "InputDirection" ).y ) );
					}
					break;
				case 40: // down arrow
				case 83: // s
					if ( tIsPressed && tObject.getAttribute( "InputDirection" ).y != 1 )
					{
						tObject.setAttribute( "InputDirection", new PointSimple( tObject.getAttribute( "InputDirection" ).x, 1 ) );
					}
					else if ( !tIsPressed && tObject.getAttribute( "InputDirection" ).y == 1 )
					{
						tObject.setAttribute( "InputDirection", new PointSimple( tObject.getAttribute( "InputDirection" ).x, 0 ) );
					}
					break;
				case 69: // e
					break;
				case 70: // f
					if ( tIsPressed && !tObject.getAttribute( "PillIsActive" ) )
					{
						tObject.setAttribute( "PillIsActive", true );
					}
					break;
				case 81: // q
					break;
				default:
					break;
			}
		}
	}
}